#!/usr/bin/env python
# encoding: utf-8

"""
@file: HengChengLi.py
@time: 2016/11/21 上午10:17
"""
# 恒成立问题

from mathsolver.functions.base import *
from mathsolver.functions.hanshu.hengchengli import HengChengLiRenYiXing

NOT_INEQ_OPS = {'>': '<=', '>=': '<', '<=': '>', '<': '>='}


def reverse_ineq_op(ineq_op):
    return NOT_INEQ_OPS[ineq_op]


# 关于x的不等式kx^{2}-6kx+k+8>0的解集为R,则实数k的取值范围是. #调用元函数同上【函数恒成立问题】
class BuDengShiHengChengLi001(BaseFunction):
    def solver(self, *args):
        arg0 = args[0]
        arg1 = args[1]
        if not (isinstance(arg0, BaseIneq) and isinstance(arg1, BaseInter)):
            raise Exception('Type Match Error')
        if arg1.value != ['(', '-∞', '∞', ')']:
            raise Exception('Type Match Error')
        solve_r = HengChengLiRenYiXing().solver(arg0)
        return solve_r


# 关于x的不等式|x-1|+|x-2|≤a^{2}+a+1的解集为空集,则实数a的取值范围是.
class BuDengShiHengChengLi002(BaseFunction):
    def solver(self, *args):
        ineq = args[0]
        text = args[1]
        if text.find('空集') < 0:
            raise Exception('Type Match Error')
        l_expr, ineq_op, r_expr = ineq.sympify()
        new_ineq = BaseIneq([str(l_expr), reverse_ineq_op(ineq_op), str(r_expr)])
        solve_r = HengChengLiRenYiXing().solver(new_ineq)
        solve_r.steps.insert(0, ['由题意得:' + new_ineq.printing(), ' 恒成立'])
        return solve_r


# 不等式恒成立的元函数
class BuDengShiHengChengLi(BaseFunction):
    CLS = [BuDengShiHengChengLi001, BuDengShiHengChengLi002]

    def solver(self, *args):
        r = None
        for cl in BuDengShiHengChengLi.CLS:
            try:
                r = cl(verbose=True).solver(*args)
                break
            except Exception:
                pass
        if not r:
            raise 'try fail'
        return r


if __name__ == '__main__':
    pass
